package com.xxl.job.admin.service.impl;

import com.google.common.eventbus.AsyncEventBus;
import com.xuxueli.api.exception.JobErrorCode;
import com.xuxueli.api.exception.JobException;
import com.xuxueli.api.service.TenantJobService;
import com.xuxueli.api.service.tenantjob.AsyncInitAddStartData;
import com.xuxueli.api.service.tenantjob.TenantJobInfo;
import com.xxl.job.admin.core.conf.EventBusFactory;
import com.xxl.job.admin.core.event.XxlJobInfoAddStartEvent;
import com.xxl.job.admin.core.model.XxlJobGroup;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.dao.XxlJobGroupDao;
import com.xxl.job.admin.dao.XxlJobInfoDao;
import com.xxl.job.admin.service.XxlJobService;
import com.xxl.job.admin.service.common.TX;
import com.xxl.job.admin.service.cvt.TenantJobInfoCvtJobInfo;
import com.xxl.job.core.biz.model.ReturnT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * description:
 * <p>
 * Date: 2019-04-30
 * <p>
 * Time: 12:07
 *
 * @author zhangxin
 */
@Service
public class TenantJobServiceImpl implements TenantJobService {

    @Resource(name = EventBusFactory.JOB_INFO_BATCH_ADD_START_BEAN_NAME)
    private AsyncEventBus batchAddStartEventBus;
    @Autowired
    private TenantJobInfoCvtJobInfo tenantJobInfoCvt;
    @Autowired
    private XxlJobService xxlJobService;
    @Resource
    private XxlJobGroupDao groupDao;
    @Resource
    private XxlJobInfoDao jobInfoDao;

    @TX
    @Override
    public List<String> batchAddStart(List<TenantJobInfo> infos)  throws JobException {
        Assert.notEmpty(infos, "infos不允许为空");

        List<String> result = new ArrayList<>();
        for(TenantJobInfo jobInfo: infos){
            XxlJobGroup group = groupDao.getByAppName(jobInfo.getServerName());
            if(group == null){
                throw new JobException(JobErrorCode.SERVER_NAME_NO_EXISTS, "job system no exists serverName: " + jobInfo.getServerName());
            }

            XxlJobInfo xxlJobInfo = tenantJobInfoCvt.cvt(jobInfo, group.getId());
            ReturnT<String> addResult = xxlJobService.add(xxlJobInfo);
            if(addResult.getCode() != ReturnT.SUCCESS_CODE){
                throw new JobException(JobErrorCode.SERVER_ERROR, addResult.getMsg());
            }

            ReturnT<String> startResult = xxlJobService.start(Integer.valueOf(addResult.getContent()).intValue());
            if(startResult.getCode() != ReturnT.SUCCESS_CODE){
                throw new JobException(JobErrorCode.SERVER_ERROR, startResult.getMsg());
            }

            result.add(addResult.getContent());
        }

        return result;
    }

    @Override
    public Boolean asyncInitAddStart(AsyncInitAddStartData data)  throws JobException{
        Assert.notNull(data, "data不允许为空");
        Assert.notNull(data.getInfo(), "info不允许为空");
        Assert.notEmpty(data.getParams(), "params不允许为空");
        Assert.notNull(data.getInfo().getServerName(), "info.serverName不允许为空");

        XxlJobGroup group = groupDao.getByAppName(data.getInfo().getServerName());
        if(group == null){
            throw new JobException(JobErrorCode.SERVER_NAME_NO_EXISTS);
        }

        int existTotal = jobInfoDao.pageListCount(0, 1, group.getId(), null, data.getInfo().getJobHandlerBeanName());
        if(existTotal > 0){
            return true;
        }

        XxlJobInfo xxlJobInfo = tenantJobInfoCvt.cvt(data.getInfo(), group.getId());
        XxlJobInfoAddStartEvent event = new XxlJobInfoAddStartEvent();
        event.setJobInfo(xxlJobInfo);
        event.setParams(data.getParams());
        batchAddStartEventBus.post(event);

        return true;
    }

}
